const userModel = require('../model/userModel')

//查询全部用户
module.exports.find = async function(searchObj,page){
    //分页查询
    let {current,size} = page
    //获取总数
    let total = await userModel.countDocuments(searchObj)
    //获取总页数
    let totalPage = Math.ceil(total / size)
    let rows = await userModel.find(searchObj).limit(parseInt(size)).skip((current-1)*size)
    return {current,size,total,totalPage,rows}
}

//根据条件查询用户
module.exports.findByUsername = async function(searchObj){
    return await userModel.find(searchObj)
}

//根据手机号查询用户
module.exports.findByPhone = async function(phoneNum){
    return await userModel.find({phoneNum:phoneNum})
}

// 根据ID查询用户
module.exports.findById = async function(id){
    return await userModel.findById({_id:id})
}

//增加用户
module.exports.insert = async function(userObj){
    return await userModel.create(userObj)
}

//删除用户
module.exports.delete = async function(id){
    return await userModel.deleteOne({_id:id})
}

//修改用户
module.exports.update = async function(id,userObj){
    return await userModel.updateOne({_id:id},userObj)
}